<template>
  <div class="login">
    <div class="title">
      <span>短视频营销</span>
      <p>短视频时代引流神器，低成本，快裂变</p>
    </div>
    <div class="content">
      <div class="carousel">
        <img class="loginBanner" src="https://source.qingyunclouds.com/login_banner001.png"
             alt=""></div>
      <div :class="['form', isReg?'reg__form':'login__form']">
        <span class="form__title">{{isReg ? '用户注册' : '用户登陆'}}</span>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username" class="el-form-item">
            <span class="form__label">账号</span>
            <el-input v-model="loginForm.username" type="text" class="login__input" auto-complete="off"
                      placeholder="请输入账号">
            </el-input>
          </el-form-item>

          <el-form-item prop="password" >
            <span class="form__label">密码</span>
            <el-input
              v-model="loginForm.password"
              type="password"
              class="login__input"
              auto-complete="off"
              placeholder="请输入密码"
              @keyup.enter.native="handleLogin"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="code" class="fhttps://open.douyin.com/platform/oauth/connect/?client_key=awyrdt49mkd11oc8&response_type=code&scope=user_info,video.create,video.data,fans.check&redirect_uri=https://dyopen.qingyunclouds.com/pages/index/index?activityId=1https://open.douyin.com/platform/oauth/connect/?client_key=抖音key&response_type=code&scope=user_info,video.create,video.data,fans.check&redirect_uri=https://dyopen.qingyunclouds.com/pages/index/index?activityId=1orm__item" v-if="captchaOnOff">
            <span class="form__label ">验证码</span>
            <div class="form__code">
              <el-input v-model="loginForm.code" type="text" class="login__input" auto-complete="off"
                        placeholder="请输入验证码">
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img"/>
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button class="login__btn"
                       :loading="loading"
                       @click.native.prevent="handleLogin"
                       type="primary">
              <span v-if="!loading">登陆</span>
              <span v-else>登陆中</span>
            </el-button>
          </el-form-item>
          <div class="login__pact" v-if="!isReg">
            <span>登陆即同意</span>
            <p>《短视频营销平台服务协议》</p>
          </div>
        </el-form>


      </div>
    </div>
  </div>
</template>
<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      isReg: false,//是否打开注册弹窗
      isCodeLogin: false,//是否扫码登陆
      forgetDialog: false,//忘记密码弹窗
      buttonName: '获取验证码',
      isDisabled: false,//获取验证码开关
      time: 80, //验证码倒计时
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  mounted() {
    //绑定事件
    window.addEventListener('keydown',this.keyDown);
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.data.captchaOnOff === undefined ? true : res.data.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.loginForm.uuid = res.data.uuid;
        }
      });
    },
    getCookie() {
      // const username = Cookies.get("username");
      // const password = Cookies.get("password");
      // const rememberMe = Cookies.get('rememberMe')
      // this.loginForm = {
      //   username: username === undefined ? this.loginForm.username : username,
      //   password: password === undefined ? this.loginForm.password : decrypt(password),
      //   rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      // };
    },
    handleLogin() {

      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          // if (this.loginForm.rememberMe) {
          //   Cookies.set("username", this.loginForm.username, { expires: 30 });
          //   Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
          //   Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          // } else {
          //   Cookies.remove("username");
          //   Cookies.remove("password");
          //   Cookies.remove('rememberMe');
          // }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    },

    keyDown(e){
            //如果是回车则执行登录方法
          if(e.keyCode == 13){
            this.handleLogin();
          }
        }
  },
  destroyed(){
    window.removeEventListener('keydown',this.keyDown,false);
  }
};
</script>

<style  lang="scss">
.form__item{
  margin-bottom: 10px;
}

.login {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
  background-image: url('../assets/images/logo-bg.png');
  position: relative;

  .loginBanner{
    display: block;

  }
  .title {
    width: 1100px;
    height: 100px;
    display: flex;
    justify-content: flex-start;
    align-items: center;

    > span {
      font-size: 30px;
      color: #427ee3;
      font-weight: bold;
    }

    > p {
      font-size: 30px;
      margin-left: 20px;
      color: #7e8cab;
    }
  }

  .content {
    width: 1100px;
    height: 580px;
    background: #fff;
    //-webkit-box-shadow:0px 0px 30px #406CA8;
    box-shadow: 0 4px 40px 0 rgba(46, 92, 182,0.5);
    display: flex;
    border-radius: 5px;

    .carousel {
      width: 660px;
      background: #000;
      height: 580px;

    }

    //登陆表单
    .login__form {
      width: 440px;
      padding: 20px;
      position: relative;

      .login__code {
        position: absolute;
        top: 50px;
        right: 30px;
        display: block;
        width: 60px;
        height: 60px;
        cursor: pointer;
      }

      .form__title {
        display: block;
        font-size: 30px;
        margin: 20px 0;
      }

      .login__input .el-input__inner {
        height: 60px;
      }
      .el-form-item__content{
        line-height: 25px;
      }

      .login__btn {
        margin-top: 20px;
      }

    }


    //表单公共数据
    .form {
      width: 440px;
      padding: 20px;
      //.form__title {
      //  display: block;
      //  font-size: 30px;
      //  margin: 10px 0;
      //}
      .form__label {
        font-size: 16px;
        color: rgba(0, 0, 0, .65);
      }

      .login__input .el-input__inner {
        border: none;
        border-bottom: 1px solid #DCDFE6;
        border-radius: 0;
        margin: 0;
        padding: 0;
      }

      .form__bottom {
        width: 400px;
        margin-top: 10px;
        display: flex;
        justify-content: space-between;

        > span {
          cursor: pointer;
        }
      }

      .login__btn {
        width: 100%;
        height: 50px;
        background: #427ee3;

      }

      .code__login {
        display: flex;
        justify-items: center;
        flex-flow: column;
        align-items: center;

        > img {
          display: block;
          width: 280px;
          height: 280px;
        }

        .code__title {
          margin-top: 20px;
        }
      }

      .form__code {
        width: 100%;
        border-bottom: 1px solid #DCDFE6;

        display: flex;
        align-items: center;

        .login__input .el-input__inner {
          border: none;
        }
      }

      .login__pact {
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 14px;

        > span {
          color: #bdbdbd;
        }

        > p {
          color: #6196f2;
          cursor: pointer;
        }
      }

      .reg__login {
        display: flex;
        justify-content: center;
        font-size: 14px;

        > span {
          cursor: pointer;
        }
      }
    }


  }
}

.forget__form {
  margin-top: 20vh;
  border-radius: 5px;

  .login__input .el-input__inner {
    border: none;
    border-bottom: 1px solid #DCDFE6;
    border-radius: 0;
    margin: 0;
    padding: 0;
  }

  .form__code {
    width: 100%;
    border-bottom: 1px solid #DCDFE6;
    display: flex;

    .login__input .el-input__inner {
      border: none;
    }
  }

  .reset__btn {
    width: 100%;
    height: 50px;
    background: #427ee3;

  }
}

.code__tips {
  color: #ffa036;
}
.login-code-img {
  height: 38px;
}
</style>
